using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Customer;
using System.Diagnostics;
using System.Threading;
using System.IO.Ports;

namespace WindowsFormsApplication1
{
    public partial class BeverageSizeSelect : Form
    {        
        string path = "C:\\Users\\eclark4\\Desktop\\WriteLines1.txt";
        string cardNum;
        Person person;
        BitArray status;
        string[] drink = new string[14];
        string size;
        int length;
        byte[] PICByte_Buffer;
        byte tempByte;
        string message;
        BitArray PICStatus;
        int bitLength;
        int limit;

        BitArray picByte = new BitArray(8);
        byte[] status_byte = new byte[1];
        public BeverageSizeSelect(Person Person, BitArray Status)
        {
            person = Person;
            cardNum = person.CardNumber;
            status = Status;
            drink = Person.Drink;
            InitializeComponent();
            
            //configuring the serial port            
            serialPort1.PortName = "COM6";
            serialPort1.BaudRate = 9600;
            serialPort1.DataBits = 8;
            serialPort1.Parity = Parity.None;
            serialPort1.StopBits = StopBits.One;                  
        }

        public void sizeClick(string Size)
        {
            size = Size;            
            status[0] = true;            
            status[1] = false;
            status[7] = false;
            status.CopyTo(status_byte, 0);            
            serialPort1.Write(status_byte, 0, 1);
        }

        private void size1_Click(object sender, EventArgs e)
        {
            // set bit 4 indicating 12oz has been chosen
            status.Set(4, true);
            status.Set(5, false);
            status.Set(6, false);
            sizeClick(Size1.Text);                         
        }

        private void Size2_Click(object sender, EventArgs e)
        {
            //set bit 5 indicating 16oz has been chosen
            status.Set(4, false);
            status.Set(5, true);
            status.Set(6, false);
            sizeClick(Size2.Text);
         }

        private void size3_Click(object sender, EventArgs e)
        {
            // set bit 6 indicating 60oz has been chosen
            status.Set(4, false);
            status.Set(5, false);
            status.Set(6, true);
            sizeClick(Size3.Text);
        }

        private void size4_Click(object sender, EventArgs e)
        {
            status.Set(4, false);
            status.Set(5, false);
            status.Set(6, false);
            sizeClick(Size4.Text);
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            length = serialPort1.BytesToRead;
            PICByte_Buffer = new byte[length];
            serialPort1.Read(PICByte_Buffer, 0, PICByte_Buffer.Length);
            PICStatus = new BitArray(PICByte_Buffer);
            bitLength = PICStatus.Length;
            limit = serialPort1.BytesToRead;
            
            char[] characters = System.Text.Encoding.ASCII.GetChars(PICByte_Buffer);
            
            tempByte = PICByte_Buffer[PICByte_Buffer.Length - 1];
                
            
            // check if the zero bit of the status byte is not a 1, this means there was an error
            if ((tempByte & 1) != 1) {
                // tell the user to chose the size again
                System.Windows.Forms.MessageBox.Show("Please Select Size Again");
            }
            else {
                serialPort1.Close();
                this.Invoke(new EventHandler(CloseForm));
                StartAndStopPour startAndStop = new StartAndStopPour(person, status, this);
                startAndStop.ShowDialog();                
            }
        }
        private void CloseForm(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BeverageSizeSelect_Load(object sender, EventArgs e)
        {
            try
            {
                if (serialPort1.IsOpen)
                {
                    serialPort1.Close();
                }
                serialPort1.Open();
                price1.Text = "$" + Convert.ToString(Math.Round((Convert.ToDecimal(drink[13]) * 2)));
                price2.Text = "$" + Convert.ToString(Math.Round((Convert.ToDecimal(drink[13]) * 4)));
                price3.Text = "$" + Convert.ToString(Math.Round((Convert.ToDecimal(drink[13]) * 6)));

            }
            catch (Exception ex)
            {
                message = ex.Message;
                //System.Windows.Forms.MessageBox.Show(message);
                MessageBox.Show("Error Sending Request to Beverage Dispeser, \n Please See Bartender For Assistance");
                this.Close();
            }  
        }

        private void helpButton_Click(object sender, EventArgs e)
        {
            string helpMessage = "Please select the size of drink that you would like to pour. " +
                "On the next screen you will be able to dispense your beverage.";
            HelpForm helpForm = new HelpForm(helpMessage);
            helpForm.ShowDialog();
        }        
    }
}